import logging


class MyLog:
    def log(self, msg, level):

        # loging 负责收集 debug info error信息
        # handdler 输出日志的渠道，到指定的文件或控制台

        # 创建收集器
        my_logging = logging.getLogger('python11')

        # 设定级别
        my_logging.setLevel('DEBUG')
        my_logging.setLevel('ERROR')

        # 设置日志格式
        # formatter = logging.Formatter('%(asctime)s-%(levelname)s-%(finename)s-%(name)s-日志信息%(message)')
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(name)s - %(message)s')

        # 创建一个我们自己的输出渠道
        ch = logging.StreamHandler()  # 输出到控制台
        ch.setLevel('ERROR')
        ch.setFormatter(formatter)


        fh = logging.FileHandler('py11.txt', encoding='utf-8')  # 输出到文件
        fh.setLevel('DEBUG')
        fh.setFormatter(formatter)



        # 指定输出
        my_logging.addHandler(ch)
        my_logging.addHandler(fh)

        # 收集
        # my_logging.debug("卧槽")
        # my_logging.error("沃德发")

        if level == 'DEBUG':
            my_logging.debug(msg)
        elif level == 'INFO':
            my_logging.info(msg)
        elif level == 'WARNING':
            my_logging.warning(msg)
        elif level == 'ERROR':
            my_logging.error(msg)
        elif level == 'CRITICAL':
            my_logging.critical(msg)

        # 关闭渠道
        my_logging.removeHandler(ch)
        my_logging.removeHandler(fh)

    def debug(self, msg):
        self.log(msg, 'DEBUG')

    def error(self, msg):
        self.log(msg, 'ERROR')


    def info(self, msg):
        self.log(msg, 'INFO')

#
# if __name__ == '__main__':
#     MyLog().log('NMB  TEST', 'ERROR')
#     MyLog().log('MTX TEST', 'INFO')
#     MyLog().log('天命不自怨，人生素有风', 'ERROR')

    # logging.debug('xiaoci今天很活跃')
    # logging.info('666')
    # logging.warning('monika')
    # logging.error('卧槽')
    # logging.critical('躺平不舒服')
